package io.summer.demo.channel;

import io.summer.gadget.channel.SummerChannel;
import io.summer.gadget.channel.core.TopicListener;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author ml
 */
@Component
public class TestSummerChannel implements InitializingBean {
  @Autowired(required = false)
  private SummerChannel summerChannel;

  @Override
  public void afterPropertiesSet() throws Exception {
    init();
  }

  @AllArgsConstructor
  @NoArgsConstructor
  public static class TestBean {
    public String value;
    public int number;
  }

  public static class TopicHandler implements TopicListener<String, TestBean> {
    @Override
    public TestBean apply(String s) {
      return new TestBean(s, s.length());
    }
  }

  void init() {
    if (summerChannel != null) {
      System.out.println("TestSummerChannel");
      summerChannel.subscribe("echo-1", new TopicHandler());
      summerChannel.subscribe("echo-2", (TopicListener<Object, Object>) o -> o);
    }
  }
}
